<template>
    <!--上传组件-->
    <div>
      <div class="l-label">
        <span class="l-label-text"><span class="l-color-red">*</span>上传照片:</span>
        <span class="l-btn-upload">上传</span>
        <input data-img="zheng,fang,complete" @change="multipleUpload" class="l-upload-hidden" type="file" multiple>
      </div>

      <div class="l-upload-img">
        <div class="l-upload-single">
          <img :src="zheng" alt="无法显示">
          <input data-img="zheng" @change="upload" type="file">
        </div>

        <div class="l-upload-single">
          <img :src="fang" alt="无法显示">
          <input data-img="fang" @change="upload" type="file">
        </div>

        <div class="l-upload-single">
          <img :src="complete" alt="无法显示">
          <input data-img="complete" @change="upload" type="file">
        </div>
      </div>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui'
    import zheng from "~/img/upload.png"
    import fang from "~/img/upload.png"
    import complete from "~/img/upload.png"
    export default {
        name: "upload",
        data () {
          return {
            zheng,
            fang,
            complete,
            imgList: {}
          }
        },
        methods: {
          upload (e) {
            if(!/image/.test(e.target.files[0].type)) {
                Toast({
                  message: "请上传图片",
                  duration: 1000,
                  position: "middle"
                })
            } else if (e.target.files[0].size >= 50000) {
              Toast({
                message: "图片大小不能超过5M",
                duration: 1000,
                position: "middle"
              })
            } else {
              let key = e.target.dataset.img
              this[key] = window.URL.createObjectURL(e.target.files[0])
              this.imgList[key] = e.target.files[0]
              this.$emit('upload', this.imgList)
            }

          },
          multipleUpload (e) {
            let filesList = [].slice.call(e.target.files)
            let keys = e.target.dataset.img.split(",")
            filesList.forEach( (item,index) => {
              if(!/image\/*/.test(item.type)) {
                Toast({
                  message: "请上传图片",
                  duration: 1000,
                  position: "middle"
                })
              } else if (item.size >= 50000) {
                Toast({
                  message: "图片大小不能超过5M",
                  duration: 1000,
                  position: "middle"
                })
              } else {
                this[keys[index]] = window.URL.createObjectURL(item)
                this.imgList[keys[index]] = e.target.files[index]
                this.$emit('upload', this.imgList)
              }
            })
          }
        }
    }
</script>

<style lang="less" scoped>
  .l-label {
    padding-left: 50px;
    color: #986d33;
    position: relative;
  }
  .l-color-red {
    color: red;
  }

  .l-label-text {
    display: inline-block;
    width: 150px;
    margin-bottom: 30px;
  }

  .l-btn-upload {
    display: inline-block;
    width: 180px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    color: #fff;
    border-radius: 45px;
    background-color: #000;
  }

  .l-upload-hidden {
    width: 150px;
    height: 50px;
    position: absolute;
    top: 0;
    left: 220px;
    opacity: 0;
  }

  .l-upload-img {
    width: 700px;
    margin: 30px auto;
    padding-left: 200px;
    display: flex;
    box-sizing: border-box;

    .l-upload-single {
      width: 162px;
      height: 114px;
      position: relative;

      input {
        position: absolute;
        top: 0;
        left: 0;
        width: inherit;
        height: inherit;
        opacity: 0;
      }

       img {
         width: inherit;
         height: inherit;
       }
    }

    .l-upload-single + .l-upload-single {
      margin-left: 10px;
    }
  }
</style>
